<template>
  <!-- 弹窗 -->
  <!-- <el-dialog :title="formData.id ? '编辑角色' : '新增角色'" :visible="showDialog" @close="closeFn"> -->
  <el-dialog :title="title" :visible="showDialog" @close="closeFn">
    <!-- showDialog: 值为true表示显示，false表示隐藏弹窗 -->
    <!-- 表单 -->
    <el-form ref="form" label-width="100px" :model="formData" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item label="头像" prop="image">
        <upload-img ref="uploadImg" />
      </el-form-item>
    </el-form>
    <!-- 插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button size="small" @click="closeFn">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployees, getEmployeesDetail, redactEmployees } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false // 默认值
    },
    title: {
      type: String,
      required: true // 是否必传
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mailbox: '', // 邮箱
        password: '', // 密码
        image: ''
      },
      rules: {
        username: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\w{4,10}$/, min: 4, max: 10, message: '长度在 4 到 10 个数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取指定员工信息
    async getEmployeesDetail(id) {
      const { data } = await getEmployeesDetail(id)
      console.log(data)
      // 数据回填
      this.formData = data
      // 头像回显
      this.$refs.uploadImg.imageUrl = this.formData.image
    },
    // 新增/编辑 员工
    async submit() {
      this.$refs.form.validate(async bool => {
        if (bool) {
          if (this.formData.id) {
            // 编辑
            // 保存头像地址
            this.formData.image = this.$refs.uploadImg.imageUrl

            await redactEmployees(this.formData)
            // 提示编辑成功
            this.$message.success('编辑成功')
          } else {
            // 新增
            // 保存头像地址
            this.formData.image = this.$refs.uploadImg.imageUrl
            // console.log(this.formData.image);
            await addEmployees(this.formData)
            // 提示新增成功
            this.$message.success('新增成功')
          }
          // 关闭弹窗
          this.closeFn()
          // 通知父组件刷新页面
          this.$emit('updateEvent')
        }
      })
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      //   清空数据
      this.formData = {
        username: '', // 用户名
        mailbox: '', // 邮箱
        password: '' // 密码
      }
      /** 清空头像信息 */
      this.$refs.uploadImg.imageUrl = ''
      //   清空校验规则
      // resetFields()对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
